import 'dart:convert';
import 'package:appflutterhomeschool/model/student_information_model.dart';
import 'package:appflutterhomeschool/utils/sp_util.dart';
import 'package:flutter/material.dart';
import 'academic_sentiment/academic_sentiment_view_model.dart';
import 'letter/letter_view_model.dart';
import 'my/my_view_model.dart';
import 'notice/notice_view_model.dart';

///全局调用方法
class MyAppViewModel extends ChangeNotifier {
  //调用方法
//  MyAppViewModel myAppViewModel = Provider.of<MyAppViewModel>(context);
//  corpViewModel.groupViewModel = viewModel;

  StudentInformationModel studentInformationModel;

  String name = "未选择";

  //通知页面
  NoticeViewModel noticeViewModel;

  //我的页面
  MyViewModel myViewModel;

  //信件页面
  LetterViewModel letterViewModel;
  AcademicSentimentViewModel academicSentimentViewModel;

  //true 是老师  false 是家长
  bool identity;

  Color color = Colors.white;

  //初始化
  initView() {
    if (SpUtil.getString("role_now") == null) {
      return;
    }
    identity = SpUtil.getString("role_now") == "TEACHER";
    String getStudent = SpUtil.getString("getStudent") ?? null;
    if (getStudent.isNotEmpty && getStudent != null) {
      studentInformationModel =
          StudentInformationModel.fromJson1(jsonDecode(getStudent));
      name = identity
          ? studentInformationModel.className
          : studentInformationModel.userName;
      // 延时1s执行返回
      Future.delayed(Duration(seconds: 5), () {
        notifyListeners();
      });
    }
  }

  //全局切换端
  globalSwitchOnTap(String swidthIdentity) async {
    SpUtil.putString("role_now", swidthIdentity).then((value) {
      if (value) {
        identity = SpUtil.getString('role_now') == "TEACHER";
        if (noticeViewModel != null) {
          noticeViewModel.getIdentityApplication();
        }
        if (letterViewModel != null) {
          letterViewModel.getIdentityApplication();
        }
        if (myViewModel != null) {
          myViewModel.getIdentityApplication();
        }
        if (academicSentimentViewModel != null) {
          academicSentimentViewModel.getIdentityApplication();
        }
        notifyListeners();
      }
    });
  }

  //需要刷新的页面
  refresh() {
    if (noticeViewModel != null) {
      noticeViewModel.getIdentityApplication();
    }
    if (letterViewModel != null) {
      letterViewModel.getIdentityApplication();
    }
    if (myViewModel != null) {
      myViewModel.getIdentityApplication();
    }
    if (academicSentimentViewModel != null) {
      academicSentimentViewModel.getIdentityApplication();
    }
  }
  refreshMyUser(){
    if (myViewModel != null) {
      myViewModel.getIdentityApplication();
    }
  }

}
